package sort;

import java.io.File;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * 索引排序
 * @author apple
 *
 */
public class IndexSort {

	public static void main(String[] args) throws Exception {
		
		FSDirectory dir = SimpleFSDirectory.open(new File("/Users/apple/lucene/index"));
		IndexReader reader = IndexReader.open(dir);
		IndexSearcher searcher = new IndexSearcher(reader);
		QueryParser parser = new QueryParser(Version.LUCENE_33, "文档", new IKAnalyzer());
		Query query = parser.parse("123");
		TopDocs t = searcher.search(query, null, 5, new Sort());
		ScoreDoc[] d = t.scoreDocs;
		for(int i = 0; i < d.length; i++){
			
			System.out.println(reader.document(d[i].doc).get("文件名"));
			System.out.println("Explanation:" + searcher.explain(query, d[i].doc));
		}
	}
}
